package com.ziyun.pop.mall;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver;
import com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

import javax.sql.DataSource;

@ComponentScan(basePackages = { "com.service.es.*", "com.service.goods.*", "com.service.order.*", "com.service.quote.*",
		"com.service.pay.*", "com.ziyun.pop.mall", "com.boot.*", "com.service.db.*", "com.service.cache.*","com.service.utils.*",
		"com.cloud.*", "com.service.cart.*", "com.service.common.*", "com.service.ucenter.*","com.service.es.*"}, excludeFilters = {
				@ComponentScan.Filter(type = FilterType.REGEX, pattern = ".*Hystrix|.*ecurityPermitAllConfig|.*Hystric|.*usEventTestListener")})
@MapperScan(basePackages = { "com.service.module.dao.mapper", "com.service.goods.dao.mapper",
		"com.service.order.dao.mapper", "com.service.cart.dao.mapper", "com.service.ucenter.dao.mapper",
		"com.service.quote.dao.mapper","com.ziyun.pop.mall.module.time.mapper"})
@EnableCaching
@EnableElasticsearchRepositories("com.service.es.repository")
@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class PopMallApplication {

	private static Logger _log = LoggerFactory.getLogger(PopMallApplication.class);

	public static void main(String[] args) {
		SpringApplication app = new SpringApplication(PopMallApplication.class);
		app.setBannerMode(Banner.Mode.OFF);
		System.setProperty("es.set.netty.runtime.available.processors", "false");
		app.run(args);
		_log.info(">>>>> POP-mall-service 启动完成 <<<<<");
	}

	private PaginationInterceptor getPaginationInterceptor() {
		PaginationInterceptor page = new PaginationInterceptor();
		page.setDialectType("mysql");
		return page;
	}

	@Bean
	public SqlSessionFactory sqlSessionFactory(DataSource dataSource, ResourceLoader resourceLoader) throws Exception {
		MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
		MybatisConfiguration configuration = new MybatisConfiguration();
		configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
		configuration.setJdbcTypeForNull(JdbcType.NULL);
		// *注册Map 下划线转驼峰*
		configuration.setObjectWrapperFactory(new MybatisMapWrapperFactory());

		sqlSessionFactory.setDataSource(dataSource);
		sqlSessionFactory.setConfiguration(configuration);
		PaginationInterceptor bean = getPaginationInterceptor();
		sqlSessionFactory.setPlugins(new PaginationInterceptor[] { bean });
		// ...其他配置
		return sqlSessionFactory.getObject();
	}




}
